package xio.ccf_201609;

/**
 * 201609-4 交通规划 100分
 * Created by Xio on 2017/10/14.
 */
import java.util.*;


public class TrafficDesign {

       public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();

        //构造图
        Graph g = new Graph(n + 1);
        for (int i = 0; i < m; i++) {
            g.addEdge(sc.nextInt(), sc.nextInt(), sc.nextInt());
        }


        //dijkstra算法
         dijkstra(1,n, g);
    }

    private static void dijkstra(int start,int n, Graph g) {
        int MAXN = 10000;
        int[] dist = new int[MAXN + 1]; //1到各点的最短路径（花费）
        int[] cost = new int[MAXN + 1]; // 用来记录最短路径中相邻节点的花费，便于最后求和
        boolean[] marked = new boolean[MAXN + 1];

        for (int i = 0; i <= n; i++) {
            dist[i] = -1; // 设为-1表示还没初始化
            cost[i] = -1;
            marked[i] = false;
        }
        dist[1] = 0;
        cost[1] = 0;

        Queue<Edge> q = new PriorityQueue<>();
        q.add(new Edge(start, 0));
        Edge f;
        while (!q.isEmpty()) {
            f = q.poll();

            int v = f.v;
            if (!marked[v]) {
                marked[v] = true;

                for (Edge e : g.adj(v)) {
                    int linkedV = e.v;
                    if (marked[linkedV]) {
                        continue;
                    }

                    int tempcost = e.cost;
                    int nextdist = dist[v] + tempcost;
                    if (dist[linkedV] == -1 || dist[linkedV] > nextdist) {
                        dist[linkedV] = nextdist;
                        cost[linkedV] = tempcost;
                        q.add(new Edge(linkedV, dist[linkedV]));
                    } else if (dist[linkedV] == nextdist)
                        if (cost[linkedV] == -1){
                            cost[linkedV] = tempcost;
                        }else {
                            cost[linkedV] = Math.min(cost[linkedV], tempcost);
                        }
                }
            }

        }

        //计算花费之和
        int result = 0;
        for (int i = 1; i <= n; i++) {
            result = result + cost[i];
        }
        System.out.println(result);
    }

    static class Graph {
        List<LinkedHashSet<Edge>> adj;

        public Graph(int v) {

            adj = new ArrayList<>();
            for (int i = 0; i < v; i++) {
                LinkedHashSet<Edge> s = new LinkedHashSet<>();
                adj.add(s);
            }
        }

        public void addEdge(int a, int b, int cost) {
            adj.get(a).add(new Edge(b, cost));
            adj.get(b).add(new Edge(a, cost));
        }

        public Iterable<Edge> adj(int v) {
            return adj.get(v);
        }
    }

    static class Edge implements Comparable<Edge>{
        int v; //目的点
        int cost;  //到这个目的点的花费

        public Edge(int v, int cost) {
            this.v = v;
            this.cost = cost;
        }

        @Override
        public int compareTo(Edge o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }
            return 0;
        }
    }
}

